package cn.eonshine.base.core.common.constant.factory;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.eonshine.base.core.common.constant.cache.Cache;
import cn.eonshine.base.core.common.constant.cache.CacheKey;
import cn.eonshine.base.core.common.constant.status.ManagerStatus;
import cn.eonshine.base.core.common.constant.status.MenuStatus;
import cn.eonshine.base.core.support.StrKit;
import cn.eonshine.base.core.util.Convert;
import cn.eonshine.base.core.util.SpringContextHolder;
import cn.eonshine.base.core.util.ToolUtil;
import cn.eonshine.cuba.inf.modular.sys.entity.Dict;
import cn.eonshine.cuba.inf.modular.sys.entity.Menu;
import cn.eonshine.cuba.inf.modular.sys.entity.Role;
import cn.eonshine.cuba.inf.modular.sys.entity.User;
import cn.eonshine.cuba.inf.modular.sys.service.IDictService;
import cn.eonshine.cuba.inf.modular.sys.service.IMenuService;
import cn.eonshine.cuba.inf.modular.sys.service.IRoleService;
import cn.eonshine.cuba.inf.modular.sys.service.IUserService;

/**
 * 常量的生产工厂
 *
 * @author fengshuonan
 * @date 2017年2月13日 下午10:55:21
 */
@Component
@DependsOn("springContextHolder")
public class ConstantFactory implements IConstantFactory {

	@Resource
    private IRoleService roleService;
	@Resource
    private IDictService dictService;
	@Resource
    private IUserService userService;
	@Resource
    private IMenuService menuService;

    public static IConstantFactory me() {
        return SpringContextHolder.getBean("constantFactory");
    }

    /**
     * 根据用户id获取用户名称
     *
     * @author stylefeng
     * @Date 2017/5/9 23:41
     */
    @Override
    public String getUserNameById(Integer userId) {
        User user = userService.getById(userId);
        if (user != null) {
            return user.getName();
        } else {
            return "--";
        }
    }

    /**
     * 根据用户id获取用户账号
     *
     * @author stylefeng
     * @date 2017年5月16日21:55:371
     */
    @Override
    public String getUserAccountById(Integer userId) {
        User user = userService.getById(userId);
        if (user != null) {
            return user.getAccount();
        } else {
            return "--";
        }
    }

    /**
     * 通过角色ids获取角色名称
     */
    @Override
    @Cacheable(value = Cache.CONSTANT, key = "'" + CacheKey.ROLES_NAME + "'+#roleIds")
    public String getRoleName(String roleIds) {
        Integer[] roles = Convert.toIntArray(roleIds);
        StringBuilder sb = new StringBuilder();
        for (int role : roles) {
            Role roleObj = roleService.getById(role);
            if (ToolUtil.isNotEmpty(roleObj) && ToolUtil.isNotEmpty(roleObj.getName())) {
                sb.append(roleObj.getName()).append(",");
            }
        }
        return StrKit.removeSuffix(sb.toString(), ",");
    }

    /**
     * 通过角色id获取角色名称
     */
    @Override
    @Cacheable(value = Cache.CONSTANT, key = "'" + CacheKey.SINGLE_ROLE_NAME + "'+#roleId")
    public String getSingleRoleName(Integer roleId) {
        if (0 == roleId) {
            return "--";
        }
        Role roleObj = roleService.getById(roleId);
        if (ToolUtil.isNotEmpty(roleObj) && ToolUtil.isNotEmpty(roleObj.getName())) {
            return roleObj.getName();
        }
        return "";
    }

    /**
     * 通过角色id获取角色英文名称
     */
    @Override
    @Cacheable(value = Cache.CONSTANT, key = "'" + CacheKey.SINGLE_ROLE_TIP + "'+#roleId")
    public String getSingleRoleTip(Integer roleId) {
        if (0 == roleId) {
            return "--";
        }
        Role roleObj = roleService.getById(roleId);
        if (ToolUtil.isNotEmpty(roleObj) && ToolUtil.isNotEmpty(roleObj.getName())) {
            return roleObj.getTips();
        }
        return "";
    }

   

    /**
     * 获取菜单的名称们(多个)
     */
    @Override
    public String getMenuNames(String menuIds) {
        Integer[] menus = Convert.toIntArray(menuIds);
        StringBuilder sb = new StringBuilder();
        for (int menu : menus) {
            Menu menuObj = menuService.getById(menu);
            if (ToolUtil.isNotEmpty(menuObj) && ToolUtil.isNotEmpty(menuObj.getName())) {
                sb.append(menuObj.getName()).append(",");
            }
        }
        return StrKit.removeSuffix(sb.toString(), ",");
    }

    /**
     * 获取菜单名称
     */
    @Override
    public String getMenuName(Long menuId) {
        if (ToolUtil.isEmpty(menuId)) {
            return "";
        } else {
            Menu menu = menuService.getById(menuId);
            if (menu == null) {
                return "";
            } else {
                return menu.getName();
            }
        }
    }

    /**
     * 获取菜单名称通过编号
     */
    @Override
    public String getMenuNameByCode(String code) {
        if (ToolUtil.isEmpty(code)) {
            return "";
        } else {
            Menu param = new Menu();
            param.setCode(code);
            QueryWrapper<Menu> ew=new QueryWrapper<Menu>();
            ew.setEntity(param);
            Menu menu = menuService.getOne(ew);
            if (menu == null) {
                return "";
            } else {
                return menu.getName();
            }
        }
    }

    /**
     * 获取字典名称
     */
    @Override
    public String getDictName(Integer dictId) {
        if (ToolUtil.isEmpty(dictId)) {
            return "";
        } else {
            Dict dict = dictService.getById(dictId);
            if (dict == null) {
                return "";
            } else {
                return dict.getName();
            }
        }
    }


    /**
     * 根据字典名称和字典中的值获取对应的名称
     */
    @Override
    public String getDictsByName(String name, Integer val) {
        Dict temp = new Dict();
        temp.setName(name);
        QueryWrapper<Dict> ew=new QueryWrapper<Dict>();
        ew.setEntity(temp);
        Dict dict = dictService.getOne(ew);
        if (dict == null) {
            return "";
        } else {
            QueryWrapper<Dict> QueryWrapper = new QueryWrapper<>();
            QueryWrapper = QueryWrapper.eq("pid", dict.getId());
            List<Dict> dicts = dictService.list(QueryWrapper);
            for (Dict item : dicts) {
                if (item.getNum() != null && item.getNum().equals(val)) {
                    return item.getName();
                }
            }
            return "";
        }
    }

    /**
     * 获取性别名称
     */
    @Override
    public String getSexName(Integer sex) {
        return getDictsByName("性别", sex);
    }

    /**
     * 获取用户登录状态
     */
    @Override
    public String getStatusName(Integer status) {
        return ManagerStatus.valueOf(status);
    }

    /**
     * 获取菜单状态
     */
    @Override
    public String getMenuStatusName(Integer status) {
        return MenuStatus.valueOf(status);
    }

    /**
     * 查询字典
     */
    @Override
    public List<Dict> findInDict(Integer id) {
        if (ToolUtil.isEmpty(id)) {
            return null;
        } else {
            QueryWrapper<Dict> QueryWrapper = new QueryWrapper<>();
            List<Dict> dicts = dictService.list(QueryWrapper.eq("pid", id));
            if (dicts == null || dicts.size() == 0) {
                return null;
            } else {
                return dicts;
            }
        }
    }

	/**   
	 * <p>Title: getDeptName</p>   
	 * <p>Description: </p>   
	 * @param deptId
	 * @return   
	 * @see cn.eonshine.base.core.common.constant.factory.IConstantFactory#getDeptName(java.lang.Integer)   
	 */
	@Override
	public String getDeptName(Integer deptId) {
		// TODO Auto-generated method stub
		return null;
	}

	/**   
	 * <p>Title: getNoticeTitle</p>   
	 * <p>Description: </p>   
	 * @param dictId
	 * @return   
	 * @see cn.eonshine.base.core.common.constant.factory.IConstantFactory#getNoticeTitle(java.lang.Integer)   
	 */
	@Override
	public String getNoticeTitle(Integer dictId) {
		// TODO Auto-generated method stub
		return null;
	}

	/**   
	 * <p>Title: getCacheObject</p>   
	 * <p>Description: </p>   
	 * @param para
	 * @return   
	 * @see cn.eonshine.base.core.common.constant.factory.IConstantFactory#getCacheObject(java.lang.String)   
	 */
	@Override
	public String getCacheObject(String para) {
		// TODO Auto-generated method stub
		return null;
	}

	/**   
	 * <p>Title: getSubDeptId</p>   
	 * <p>Description: </p>   
	 * @param deptid
	 * @return   
	 * @see cn.eonshine.base.core.common.constant.factory.IConstantFactory#getSubDeptId(java.lang.Integer)   
	 */
	@Override
	public List<Integer> getSubDeptId(Integer deptid) {
		// TODO Auto-generated method stub
		return null;
	}

	/**   
	 * <p>Title: getParentDeptIds</p>   
	 * <p>Description: </p>   
	 * @param deptid
	 * @return   
	 * @see cn.eonshine.base.core.common.constant.factory.IConstantFactory#getParentDeptIds(java.lang.Integer)   
	 */
	@Override
	public List<Integer> getParentDeptIds(Integer deptid) {
		// TODO Auto-generated method stub
		return null;
	}

}
